﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace PmfWeb.Core.WebControls
{
    ///<summary>
    /// 提示控件
    ///</summary>
    [DefaultEvent("Click"),DefaultProperty("Text"),ToolboxItem("<{0}:Hint runat=server />")]
    public class Hint : System.Web.UI.WebControls.WebControl
    {
        private string _imgUrl = "";

        private bool isShow = false;
        ///<summary>
        /// 图片属性
        ///</summary>
        [Bindable(true), Category("Appearance"),DefaultValue("../images/")]
        public string ImgUrl
        {

            get { return _imgUrl; }
            set { _imgUrl = value; }
        }

        ///<summary>
        /// 是否显示该控件
        ///</summary>
        [Bindable(true), Category("Appearance"),DefaultValue(false)]
        public bool IsShow
        {
            get
            {
                return isShow;
            }
            set
            {
                isShow = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            Page.RegisterRequiresControlState(this);
            base.OnInit(e);
        }

        protected override void LoadControlState(object savedState)
        {
            _imgUrl = savedState.ToString();
            //base.LoadControlState(savedState);
        }

        protected override object SaveControlState()
        {
            return _imgUrl;
            //return base.SaveControlState();
        }



        /// <summary>
        /// 输出html,在浏览器中显示控件
        /// </summary>
        /// <param name="output"></param>
        protected override void Render(HtmlTextWriter output)
        {
            if(!isShow) return;
            string strbg1 = this.Page.ClientScript.GetWebResourceUrl(typeof (Hint), "Common.resource.commandbg.gif");
            string strbg2 = this.Page.ClientScript.GetWebResourceUrl(typeof (Hint), "Common.resource.commandbg2.gif");
            string strbg3 = this.Page.ClientScript.GetWebResourceUrl(typeof (Hint), "Common.resource.commandbg3.gif");
            string strbg4 = this.Page.ClientScript.GetWebResourceUrl(typeof (Hint), "Common.resource.commandbg4.gif");
            string strbg5 = this.Page.ClientScript.GetWebResourceUrl(typeof (Hint), "Common.resource.commandbg5.gif");
            string strdot = this.Page.ClientScript.GetWebResourceUrl(typeof (Hint), "Common.resource.dot.gif");


            StringBuilder sb = new StringBuilder();
            sb.Append("<!--提示层部分开始-->");
            sb.Append("<div id=\"Hint_Div\" style=\"display:none;\"");
            sb.Append(
                "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"table-layout: fixed;overflow: hidden;text-overflow: ellipsis; word-wrap: break-word\">");
            sb.Append(" <tr><td width=\"271\" height=\"7\" id=\"pmf_hint_hintTop\" colspan=\"4\" class=\"pmf_hint_mess_top\">");
            sb.Append("</td></tr>");
            sb.Append("<tr>");
            sb.Append(System.String.Format("<td colspan=\"4\" bgcolor=\"#FFFFE1\" class=\"pmf_hint_mess_mod\"><img src=\"{0}\" width=\"11\" height=\"13\" align=\"left\" style=\"margin-right:10px;\" />", strdot));
            sb.Append("<!--要呈现的提示文字开始-->");
            sb.Append("<span  id=\"pmf_hint\">");
            sb.Append("飘渺峰提示控件");
            sb.Append("</span>");
            sb.Append("<!-要呈现的提示文字信息结束-->");
            sb.Append("</td></tr><tr>");
            sb.Append("<td height=\"22\" colspan=\"4\" id=\"pmf_hint_hintBottom\" class=\"pmf_hint_mess_down\"></td>");
            sb.Append("    </tr></table></div>");
            sb.Append("<!--提示层部分结束-->\r\n");
            output.Write(sb.ToString());
        }
        protected override void OnPreRender(EventArgs e)
        {
            if (null != this.Page)
            {
                if(!Page.ClientScript.IsStartupScriptRegistered("common_resource_core_js"))
                    this.Page.ClientScript.RegisterClientScriptResource(typeof(Input), "Common.resource.core.js");
                if (!Page.ClientScript.IsStartupScriptRegistered("common_resource_bgiframe_js"))
                this.Page.ClientScript.RegisterClientScriptResource(typeof(Hint), "Common.resource.bgiframe.js");
                if (!Page.ClientScript.IsStartupScriptRegistered("common_resource_hint_js"))
                this.Page.ClientScript.RegisterClientScriptResource(typeof(Hint), "Common.resource.hint.js");

                RegisterClientCssResource("Common.resource.core.css");
            }
            base.OnPreRender(e);
        }
        protected void RegisterClientCssResource(string path)
        {
            if(null != this.Page.Header)
            {
                foreach (Control c in this.Page.Header.Controls)
                {
                    if(c.ID == "core_css")
                    {
                        return;
                    }
                }
                HtmlGenericControl control = new HtmlGenericControl("link");
                control.Attributes["href"] = Page.ClientScript.GetWebResourceUrl(typeof(Input), path);
                control.Attributes["id"] = "core_css";
                control.Attributes["type"] = "text/css";
                control.Attributes["rel"] = "stylesheet";
                Page.Header.Controls.Add(control);
            }


        }
             
    }
}
